<?php

namespace app\admin\controller;

class file
{
    //上传文件
    public static function uploadFile($fileName,$path)
    {
        // 检查是否有文件上传
        $file = \think\facade\Request::file($fileName);
        if (!$file) {
            return show(config("status.error"), "请上传文件");
        }
        // 文件保存路径配置
        $saveName = \think\facade\Filesystem::disk('public')->putFile($path, $file);
        // 获取访问URL
        $accessUrl = \think\facade\Filesystem::disk('public')->url($saveName);
        // 返回上传信息
        return $accessUrl;
    }

    public static function uploadFiles($fileName,$path){
        // 初始化一个数组来保存上传后的文件信息
        $uploadedFiles = [];
        // 获取所有上传的文件（字段名为images[]）
        if (!empty($_FILES[$fileName]['name']) && is_array($_FILES[$fileName]['name'])) {
            foreach ($_FILES[$fileName]['error'] as $key => $error) {
                if ($error == UPLOAD_ERR_OK) {
                    $file = request()->file($fileName.'.'.$key);// 或者直接使用 $_FILES[$fileName']['tmp_name'][$key]
                    $saveName = \think\facade\Filesystem::disk('public')->putFile($path, $file);
                    if ($saveName) {
                        $uploadedFiles[] = \think\facade\Filesystem::disk('public')->url($saveName);
                    }
                }
            }
        }
        return $uploadedFiles;
    }
}
